﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
using System.Windows.Forms;
//new
using System.IO;

namespace JKZP
{
    public partial class editcamera : Form
    {
        public string roadid;
        public string camroad;
        public string camip;
        public string camtype;
        public string camname;
        public string camposition;
        public string cambz;
        public string camdeviceid;
        public string username;
        public string password;
        public int rowindex;
        public editcamera()
        {
            InitializeComponent();
        }

        private void editcamera_Load(object sender, EventArgs e)
        {
            try
            {
                this.comboBox_camtype.Items.Clear();
                foreach (string type in Enum.GetNames(typeof(PP.JKCAMERATYPE)))
                {
                    this.comboBox_camtype.Items.Add(type);
                }

                this.comboBox_camposition.Items.Clear();
                foreach (string position in Enum.GetNames(typeof(PP.CAMERAPOSITION)))
                {
                    this.comboBox_camposition.Items.Add(position);
                }


                if (PP.gRoadList != null)
                {
                    this.comboBox_camroad.Items.Clear();
                    foreach (PP.RoadParam rp in PP.gRoadList)
                    {
                        this.comboBox_camroad.Items.Add(rp.LKID + "---" + rp.LKMC);
                    }
                }


                comboBox_camroad.Text = roadid + "---" + camroad;
                textBox_camname.Text = camname;
                comboBox_camtype.Text = camtype;
                textBox_camip.Text = camip;
                comboBox_camposition.Text = camposition;
                textBox_cambz.Text = cambz;
                textBox_deviceid.Text = camdeviceid;
                textBox_username.Text = username;
                textBox_password.Text = password;
            }
            catch { }
        }

        private void button_save_Click(object sender, EventArgs e)
        {
            if (comboBox_camroad.Text == "")
            {
                toolStripStatusLabel1.Text = "编辑失败，未选择所属路口";
                return;
            }
            if (textBox_camname.Text == "")
            {
                toolStripStatusLabel1.Text = "编辑失败，未填写摄像机名称";
                return;
            }
            if (comboBox_camtype.Text == "")
            {
                toolStripStatusLabel1.Text = "编辑失败，未选择摄像机类型";
                return;
            }
            if (textBox_camip.Text == "")
            {
                toolStripStatusLabel1.Text = "编辑失败，未填写摄像机IP";
                return;
            }
            if (comboBox_camposition.Text == "")
            {
                toolStripStatusLabel1.Text = "编辑失败，未选择方向";
                return;
            }
            if (textBox_deviceid.Text == "")
            {
                toolStripStatusLabel1.Text = "编辑失败，未填写设备编号";
                return;
            }
            if (textBox_username.Text == "")
            {
                toolStripStatusLabel1.Text = "编辑失败，未填写用户名";
                return;
            }
            if (textBox_password.Text == "")
            {
                toolStripStatusLabel1.Text = "编辑失败，未填写密码";
                return;
            }

            DataGridView dgv = ((CameraForm)(this.Owner)).dataGridView1;
            for (int i = 0; i < dgv.Rows.Count; i++)
            {
                DataGridViewRow r = dgv.Rows[i];
                if (textBox_camip.Text == r.Cells["camip"].Value.ToString() && (i != rowindex))
                {
                    toolStripStatusLabel1.Text = "编辑失败，摄像机IP已存在";
                    return;
                }
            }
            string[] camroad = comboBox_camroad.Text.Split(new string[] { "---" }, StringSplitOptions.RemoveEmptyEntries);
            if (EditCam(textBox_camname.Text, textBox_camip.Text, comboBox_camtype.Text, comboBox_camposition.Text, textBox_cambz.Text, textBox_deviceid.Text,textBox_username.Text,textBox_password.Text) == true)
            {
                dgv.Rows[rowindex].Cells["roadid"].Value = camroad[0];
                dgv.Rows[rowindex].Cells["camroad"].Value = camroad[1];
                dgv.Rows[rowindex].Cells["camip"].Value = textBox_camip.Text;
                dgv.Rows[rowindex].Cells["camtype"].Value = comboBox_camtype.Text;
                dgv.Rows[rowindex].Cells["camname"].Value = textBox_camname.Text;
                dgv.Rows[rowindex].Cells["camposition"].Value = comboBox_camposition.Text;
                dgv.Rows[rowindex].Cells["cambz"].Value = textBox_cambz.Text;
                dgv.Rows[rowindex].Cells["deviceid"].Value = textBox_deviceid.Text;
                dgv.Rows[rowindex].Cells["username"].Value = textBox_username.Text;
                dgv.Rows[rowindex].Cells["password"].Value = textBox_password.Text;
                ((Form_Main)this.Owner.Owner).UpDateTree();

                if (PP.gISWebVersion == false)
                {
                    toolStripStatusLabel1.Text = "修改成功";
                }
                else
                {
                    if (PP.kdws.JKZP_UploadRoad(File.ReadAllText(PP.gRoadFile)) == true)
                    {
                        PP.ReLoad();
                        toolStripStatusLabel1.Text = "修改成功";
                    }
                    else
                    {
                        toolStripStatusLabel1.Text = "修改失败";
                    }
                }
            }
            else
            {
                toolStripStatusLabel1.Text = "修改失败";
            }
        }

        private void button_exit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private bool EditCam(string camname,string camip, string camtype, string camposition,  string bz,string deviceid,string username,string password)
        {
            //<road roadid="20" gbdm="" roadname="测试路口">
    //<camera cameraname="测试路口东球机" ip="192.168.1.108" type="DAHUA" position="东" bz="">

            string xpath = "descendant::camera[@ip='" + camip + "']";
            string[] key = new string[8];
            string[] value = new string[8];
            key[0] = "cameraname";
            key[1] = "ip";
            key[2] = "type";
            key[3] = "position";
            key[4] = "bz";
            key[5] = "deviceid";
            key[6] = "username";
            key[7] = "password";
            value[0] = camname;
            value[1] = camip;
            value[2] = camtype;
            value[3] = camposition;
            value[4] = bz;
            value[5] = deviceid;
            value[6] = username;
            value[7] = password;
            bool ans = PP.SetConfig(PP.gRoadFile, xpath, "", key, value);
            if (ans == true)
            {
                PP.EditCam(camname,camip, camtype,camposition, bz,deviceid,username,password);
            }
            return ans;
        }

    }
}
